package com.totoro.metrics;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;

import java.util.concurrent.TimeUnit;

/**
 * Meters用来度量某个时间段的平均处理次数。如TPS
 * 通过metrics.meter()实例化一个Meter之后，然后通过meter.mark()方法就能将本次请求记录下来。统计结果有总的请求数，平均每秒的请求数，以及最近的1、5、15分钟的平均TPS。
 * Created by Niki on 2018/8/28 14:34
 */
public class MetersDemo {
    private static final MetricRegistry metrics = new MetricRegistry();

    private static final Meter requests = metrics.meter(MetricRegistry.name(MetersDemo.class, "request"));

    public static void handleRequest() {
        requests.mark();
    }



    public static void main(String[] args) throws InterruptedException {
        OutputUtil.console(metrics);
        int i = 0;
        while (i < 500) {
            i++;
            handleRequest();
            Thread.sleep(1000);
        }
    }
    /*
    -- Meters ----------------------------------------------------------------------
com.totoro.metrics.MetersDemo.request
             count = 6
         mean rate = 0.99 events/second
     1-minute rate = 1.00 events/second
     5-minute rate = 1.00 events/second
    15-minute rate = 1.00 events/second
     */
}
